package com.hykj.coca.control;


import com.alibaba.fastjson.JSON;
import com.hykj.coca.model.AdminModel;
import com.hykj.coca.result.AppBack;
import com.hykj.coca.service.AdminService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@EnableAutoConfiguration
@RequestMapping("/webAPI")
public class WebApi {

    private Logger logger = Logger.getLogger(WebApi.class);

    @Autowired
    private AdminService adminService;

    @RequestMapping("/login")
    @ResponseBody
    public AppBack login(HttpServletRequest request, String userName, String password){
        AdminModel adminModel=  adminService.login(userName,password);
        if (adminModel == null) {
            return new AppBack<>(-1, "用户名或密码错误");
        }
        request.getSession().invalidate();
        request.getSession().setAttribute("adminModel",adminModel);
        logger.info(JSON.toJSONString(adminModel));
        return new AppBack();

    }

}
